
export class switchBot extends plugin {
  constructor () {
    super({
      name: '小月开关Bot',
      event: 'message.group',
      priority: -10,
      rule: [{
        reg: '^#?小月醒来$',
        fnc: 'open',
        permission: 'master'
      },
      {
        reg: '^#?小月睡觉$',
        fnc: 'close',
        permission: 'master'
      },
      {
        reg: '',
        fnc: 'monitor',
        log: false
      }
      ]
    })
    this.data = null
    this.key = 'xioayue:switchBot:data'
    this.defCfg = {
      switch: true
    }
  }

  // 开启
  async open (e) {
    await this.getData()
    this.data[e.group_id].switch = true
    this.writeData()
    e.reply('小月会好好听话哒！')
  }

  // 关闭
  async close (e) {
    await this.getData()
    this.data[e.group_id].switch = false
    this.writeData()
    e.reply('小月不要理你们啦！')
  }

  async monitor (e) {
    await this.getData()
    if (!this.data[e.group_id] || this.data[e.group_id].switch || this.e.isMaster) {
      return false
    } else if (!this.data[e.group_id].switch) {
      return true
    }
  }

  async getData () {
    if (this.data) {
      this.data[this.e.group_id] ||= this.defCfg
      return this.data
    }
    this.data = JSON.parse(await redis.get(this.key)) ?? {}
    this.data[this.e.group_id] ||= this.defCfg
  }

  async writeData () {
    return redis.set(this.key, JSON.stringify(this.data))
  }
}
